package com.xdog.okx.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.alibaba.fastjson.JSONObject;
import com.xdog.common.core.domain.R;
import com.xdog.okx.domain.BlockQuery;
import com.xdog.okx.domain.block.BlockListQuery;
import com.xdog.okx.domain.block.BlockTransactionListQuery;
import com.xdog.okx.domain.block.BlockTransactionListMultiQuery;
import com.xdog.okx.service.IOkxBlockService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * OKX 区块链基础数据控制器
 */
@Tag(name = "OKX 区块链基础数据", description = "OKX 区块链基础数据控制器")
@RestController
@RequestMapping("/okx/block")
public class OkxBlockController {

    @Resource
    private IOkxBlockService okxBlockService;

    /**
     * 获取区块详情
     */
    @Operation(summary = "获取区块详情", description = "根据链短名称和区块高度获取区块详情")
    @GetMapping("/details")
    public R<JSONObject> getBlockDetails(BlockQuery query) {
        try {
            JSONObject blockDetails = okxBlockService.getBlockDetails(query.getChainShortName(), query.getHeight());
            return R.ok(blockDetails);
        } catch (Exception e) {
            return R.fail("获取区块详情失败: " + e.getMessage());
        }
    }

    /**
     * 查询区块列表
     */
    @Operation(summary = "查询区块列表", description = "根据链短名称查询区块列表")
    @GetMapping("/list")
    @SaIgnore
    public R<JSONObject> getBlockList(BlockListQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            if (query.getHeight() != null) params.put("height", query.getHeight());
            if (query.getLimit() != null) params.put("limit", query.getLimit());
            if (query.getPage() != null) params.put("page", query.getPage());

            JSONObject blockList = okxBlockService.getBlockList(params);
            return R.ok(blockList);
        } catch (Exception e) {
            return R.fail("查询区块列表失败: " + e.getMessage());
        }
    }

    /**
     * 查询区块交易列表
     */
    @Operation(summary = "查询区块交易列表", description = "根据链短名称和区块高度查询区块交易列表")
    @GetMapping("/transaction-list")
    public R<JSONObject> getBlockTransactionList(BlockTransactionListQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            params.put("height", query.getHeight());
            if (query.getProtocolType() != null) params.put("protocolType", query.getProtocolType());
            if (query.getLimit() != null) params.put("limit", query.getLimit());
            if (query.getPage() != null) params.put("page", query.getPage());

            JSONObject transactionList = okxBlockService.getBlockTransactionList(params);
            return R.ok(transactionList);
        } catch (Exception e) {
            return R.fail("查询区块交易列表失败: " + e.getMessage());
        }
    }

    /**
     * 批量查询区块交易列表
     */
    @Operation(summary = "批量查询区块交易列表", description = "根据链短名称和区块高度范围批量查询区块交易列表")
    @GetMapping("/transaction-list-multi")
    public R<JSONObject> getBlockTransactionListMulti(BlockTransactionListMultiQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            params.put("startBlockHeight", query.getStartBlockHeight());
            params.put("endBlockHeight", query.getEndBlockHeight());
            if (query.getProtocolType() != null) params.put("protocolType", query.getProtocolType());
            if (query.getLimit() != null) params.put("limit", query.getLimit());
            if (query.getPage() != null) params.put("page", query.getPage());

            JSONObject transactionListMulti = okxBlockService.getBlockTransactionListMulti(params);
            return R.ok(transactionListMulti);
        } catch (Exception e) {
            return R.fail("批量查询区块交易列表失败: " + e.getMessage());
        }
    }
}
